package quickAdd.client;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.SourcesTableEvents;

import java.util.List;

/**
 * Date: Mar 1, 2007 Time: 12:17:34 PM
 *
 * @author pat
 */
public class SpreadsheetsTable extends FlexTable implements TableListener
{
    private int selectedRow = -1;

    public SpreadsheetsTable( String title )
    {
        // Create the header row.
        setText( 0, 0, title );
        getRowFormatter().setStyleName( 0, "quickadd-ListHeader" );

        // Initialize the rest of the rows.
        setText( 1, 0, "." );
        //setText( 2, 0, "." );
        //setText( 3, 0, "." );

        addTableListener( this );
        setStyleName( "quickadd-SpreadsheetTable ");
    }

    public void setRow( int i, String title ) {
        setText( i+1, 0, title );
    }

    public void setTitles( List titles )
    {
        for ( int i = 0; i < titles.size(); i++ )
        {
            String title = (String)titles.get( i );
            setText( i+1, 0, title );
        }
    }

    public void deselect()
    {
        styleRow( selectedRow, false );
        selectedRow = -1;
    }
    public void selectRow( int row )
    {
        row+=1;
        styleRow( selectedRow, false );
        styleRow( row, true );
        selectedRow = row;
    }

    public void onCellClicked( SourcesTableEvents sender, int row, int cell )
    {
        System.out.println( "cell clicked" );
        // Select the row that was clicked (-1 to account for header row).
        if ( row > 0 ) {
            selectRow( row - 1 );
        }
    }

    private void styleRow( int row, boolean selected )
    {
        if ( row == -1 )
            return;
        if ( selected ) {
            getRowFormatter().addStyleName( row, "quickadd-SelectedRow" );
        } else {
            getRowFormatter().removeStyleName( row, "quickadd-SelectedRow" );
        }
    }

    public void clearData() {
        deselect();
        int rows = getRowCount();
        for(int i=0; i< rows-1; i++) {
            // any diff?
            //setText(i+1,0, "");
            clearCell(i+1,0);
        }
    }


}
